package com.example.work3;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.work3.Fragment.Weather;
import com.example.work3.Fragment.WeatherAdapter;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class CityWeather_Activity extends AppCompatActivity {
    private WeatherAdapter weatherAdapter;
    OkHttpClient okHttpClient = new OkHttpClient();
    private String nian;
    private String yue;
    private String ri;
    private String city;

    private String date;
    private String temp;
    private String windDir;
    private String windLevel;
    private RecyclerView ry_one;
    private TextView chengshi;

    private List<Weather> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_blank4);
        Intent intent=getIntent();
        city = intent.getStringExtra("city");

        Log.i("chengshi", city);
        iniWeather();
        iniRec();

        chengshi.setText(city);

    }

    private void iniRec(){
        chengshi = findViewById(R.id.chengshi);

        ry_one = findViewById(R.id.ry_one);
        weatherAdapter = new WeatherAdapter(list);
        ry_one.setAdapter(weatherAdapter);
        ry_one.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        ry_one.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

    }

    private void iniWeather(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                RequestBody requestBody = new FormBody.Builder()
                        .add("apiKey", "ijDE2g5a3e35965ca638f1115d95e6d51a0155f658307e9")
                        .add("area",city)
                        .build();
                Request request = new Request.Builder()

                        .url("https://api.apishop.net/common/weather/get15DaysWeatherByArea")
                        .post(requestBody)
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String result = response.body().string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                Condition sixDay = gson.fromJson(result,Condition.class);
                                for (int i = 0; i < 6; i++){
                                    nian = String.valueOf(sixDay.getResult().getDayList().get(i).getDaytime().substring(0,4)+"年");
                                    yue = String.valueOf(sixDay.getResult().getDayList().get(i).getDaytime().substring(5,6)+"月");
                                    ri = String.valueOf(sixDay.getResult().getDayList().get(i).getDaytime().substring(7,8)+"日");
                                    date = nian+yue+ri;
                                    temp = String.valueOf(sixDay.getResult().getDayList().get(i).getDay_air_temperature()+"°C");
                                    windDir = String.valueOf(sixDay.getResult().getDayList().get(i).getDay_wind_direction());
                                    windLevel = String.valueOf(sixDay.getResult().getDayList().get(i).getDay_wind_power());
                                    list.add(new Weather(date,temp,windDir,windLevel));
                                }
                                weatherAdapter.notifyDataSetChanged();
                            }
                        });
                    }
                });

            }
        }).start();
    }
}
